<?php

namespace Application\Controllers;


use NOS\Core\NosException;
use NOS\NosClient;
use Phalcon\Mvc\Controller;

class PublicController extends Controller
{
    /**
     * @var NosClient
     */
    private $nos = null;
    private $bucket = 'blog-bucket';
    private $asseturl = "http://assets.mirchen.com/";

    public function initialize()
    {
        $this->nos = new NosClient(
            'ba46ff645f6a476b9ecf13e31b426277', //ACCESS_KEY
            'eb6368e1cc0143ad915a6b2c80864e20', //ACCESS_SECRET
            'nos-eastchina1.126.net'    //END_POINT
        );
        $this->nos->setTimeout(3600 /* seconds */);
        $this->nos->setConnectTimeout(10 /* seconds */);
    }

    public function uploadAction()
    {
        $from = $this->request->get('from', null, '');
        if ($this->request->isPost()) {
            if ($this->request->hasFiles()) {
                foreach ($this->request->getUploadedFiles() as $file) {
                    $random = substr(md5($file->getName() . $file->getKey() . $file->getExtension()), 0, 16);
                    $path = "blog/uploads/" . date("Y/m/d") . '/' . $random . '.' . $file->getExtension();
                    try {
                        $this->nos->putObject($this->bucket, $path, file_get_contents($file->getTempName()));
                        if ($from == "editor") {
                            return $this->response->setJsonContent(['success' => 1, 'message' => '上传成功', 'url' => $this->asseturl . $path]);
                        } else {
                            return $this->response->setJsonContent(['errcode' => 0, 'url' => $this->asseturl . $path]);
                        }
                    } catch (NosException $exception) {
                        if ($from == "editor") {
                            return $this->response->setJsonContent(['success' => 0, 'message' => '上传失败:' . $exception->getMessage(), 'url' => '']);
                        } else {
                            return $this->response->setJsonContent(['errcode' => 1, 'errmsg' => $exception->getMessage()]);
                        }
                    }
                }
            }
            if ($from == "editor") {
                return $this->response->setJsonContent(['success' => 0, 'message' => '没有找到上传文件', 'url' => '']);
            } else {
                return $this->response->setJsonContent(['error' => 1]);
            }

        }
        $catid = $this->request->get('catid', null, 0);
        $callback = $this->request->get('callback', null, '');
        $num = (int)$this->request->get('num', null, 0);

        if (!$callback) {
            return $this->response->setJsonContent(['error' => '没有找到callback']);
        }
        $artid = $this->request->get('artid', null, 0);
        if (!$catid && !$artid && !$from) {
            return $this->response->setJsonContent(['error' => '没有找到catid和artid和from']);
        }
        $this->view->setVar('catid', $catid);
        $this->view->setVar('artid', $artid);
        $this->view->setVar('callback', $callback);
        $this->view->setVar('imagenum', $num);
    }
}